#include <QMessageBox>
#include <QtPlugin>

#include "text.h"

QString TextCodec::escape(const char &ch)
{
	if ( (ch == 13) || (ch == 10) ) return QString("<br/>");
	if ( ch < 32 ) return QString(".");
	if ( ch == '<' ) return QString("&lt;");
	if ( ch == '>' ) return QString("&gt;");
	if ( ch == '&' ) return QString("&amp;");
	if ( ch == '\"' ) return QString("&quot;");
	if ( ch == '\'' ) return QString("&apos;");

	return QString(ch);
}

TextCodec::TextCodec()
{
	// nothin'
}

TextCodec::~TextCodec()
{
	// nothin' too :)
}

// decode data
QString TextCodec::decode(const QByteArray& ba, const bool toServer)
{
	QString str;
	for (int i=0; i<ba.size(); ++i)
		str += escape(ba.at(i));

	// change color
	if (toServer) str = "<font color=\"red\">"  + str + "</font>"; // client->server - red color
	else          str = "<font color=\"green\">"+ str + "</font>"; // server->client - green color
	str = "<p>" + str + "</p>";
	return str;
}

// encode data for send
QByteArray TextCodec::encode(const QString& str, const bool toServer)
{
	//TODO
	return QByteArray();
}

// returns codec short name for menu
QString TextCodec::codecName()
{
	return QString("text");
}

// return codec description
QString TextCodec::codecDesc()
{
	return QString("simple codec for text-based protocols");
}

TParams TextCodec::params()
{
	// no params
	TParams t;
	return t;
}

void TextCodec::setParams(TParams&)
{
	// ignore
}

void TextCodec::showParamsDialog()
{
	QString text = "This is <b>text</b> codec.<br/>";
		text+= "<font color=\"red\">Keep in mind that it can correctly work only with Latin-1 text-based protocols.</font>";
		text+= "For other porpuses you may want to use 'hex' codec or develop your own from this skeleton.<br/><br/>";
		text+= "No parameters avaible for this codec.";
	QMessageBox::about(0, "text", text);
}

// Change for your file name (TARGET in .pro) and class name
Q_EXPORT_PLUGIN2(text, TextCodec)

// kate: space-indent off; tab-indents on; indent-width 4; indent-mode cstyle;
